package com.virgo.demo.cms.controller.admin;

import com.virgo.common.entity.IResponse;
import com.virgo.common.entity.ListDataVo;
import com.virgo.demo.cms.domain.bo.ArticleBo;
import com.virgo.demo.cms.domain.vo.ArticleInfoVo;
import com.virgo.demo.cms.domain.vo.ArticleListVo;
import com.virgo.demo.cms.service.ArticleService;
import com.virgo.demo.core.annotation.Log;
import com.virgo.demo.core.consts.SiteConst;
import com.virgo.limit.annotation.RepeatSubmit;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manage168/article")
@RequiredArgsConstructor
public class ArticleController {

	private final ArticleService articleService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@Log(module = "文章", type = "编辑", permission = "article_save")
	@RepeatSubmit
	public IResponse<Void> saveAction(@Validated ArticleBo articleBo) {
		articleService.saveArticle(articleBo);
		return IResponse.ok();
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@Log(module = "文章", type = "删除", permission = "article_del")
	@RepeatSubmit
	public IResponse<Void> delAction(HttpServletRequest request) {
		String ids = IRequestUtils.getString(request,"ids","");
		if (ObjectUtils.isEmpty(ids)) {
			return IResponse.fail("参数错误");
		}
		articleService.deleteArticle(ids);
		return IResponse.ok();
	}

	@RequestMapping(value = "/status", method = RequestMethod.POST)
	@Log(module = "文章", type = "编辑", permission = "article_status")
	@RepeatSubmit
	public IResponse<Void> statusAction(HttpServletRequest request) {
		Long id = IRequestUtils.getLong(request,"id");
		Integer status = IRequestUtils.getInteger(request,"status");
		if (ObjectUtils.isEmpty(id) || id.compareTo(0L) < 1 || !SiteConst.yesOrNoList.contains(status)) {
			return IResponse.fail("参数错误");
		}
		articleService.updateStatus(id,status);
		return IResponse.ok();
	}

	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@Log(permission = "article_save", saveLog = false)
	public IResponse<ArticleInfoVo> infoAction(HttpServletRequest request) {
		Long id = IRequestUtils.getLong(request,"id");
		if (ObjectUtils.isEmpty(id) || id.compareTo(0L) < 1 ) {
			return IResponse.fail("参数错误");
		}
		return IResponse.ok(articleService.findInfo(id));
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@Log(permission = "article_list", saveLog = false)
	public IResponse<ListDataVo<ArticleListVo>> listAction(HttpServletRequest request) {
		return IResponse.ok(articleService.findList(request));
	}
}